package com.hrt.manage.resource.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.hrt.base.BaseDaoImpl;
import com.hrt.bean.Resources;
import com.hrt.manage.resource.dao.ResourceDao;

/**
 * 
 * @author Administrator
 *
 */
@Repository
public class ResourceDaoImpl extends BaseDaoImpl<Resources> implements
		ResourceDao {

	public ResourceDaoImpl() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * 加载产品资源
	 */
	@Override
	public List<Resources> getProductResource(Long proId, Integer isgift,
			Integer resType, Integer pageIndex, Integer pageSize) {
		List condition = new ArrayList();
		Map proMap = new HashMap();
		proMap.put("field", "proId");
		proMap.put("value", proId);
		Map resTypeMap = new HashMap();
		resTypeMap.put("field", "resType");
		resTypeMap.put("value", resType);
		Map typeMap = new HashMap();
		typeMap.put("field", "isgift");
		typeMap.put("value", isgift);
		Map delcon = new HashMap();
		delcon.put("field", "isdelete");
		delcon.put("value", 0);
		condition.add(delcon);
		condition.add(typeMap);
		condition.add(proMap);
		condition.add(resTypeMap);
		return queryPageByCondition(condition, pageIndex, pageSize);
	}

	@Override
	public List<Resources> queryPageByCondition(List conditions, int pageIndex,
			int pageSize) {
		// TODO Auto-generated method stub
		List<Resources> list = new ArrayList<Resources>();
		String hql = "select r from Resources r";

		Map delcon = new HashMap();
		delcon.put("field", "isdelete");
		delcon.put("value", 0);
		conditions.add(delcon);
		for (int i = 0; i < conditions.size(); i++) {
			Map condition = (Map) conditions.get(i);
			String field = condition.get("field").toString();
			Object val = condition.get("value");
			if (i == 0) {
				hql = hql + " where r." + field + "=" + val;
			} else {
				hql = hql + " and r." + field + "=" + val;
			}
		}

		list = this.queryByPage(hql, pageIndex, pageSize);
		return list;
	}

	@Override
	public List<Resources> getProductResource(Long proId,
			Integer restype) {
		List<Resources> list = new ArrayList<Resources>();
		String hql = "select r from Resources r where r.isgift="
				+1 + " and r.proId=" + proId
				+ "and r.resType="+restype+" and r.isdelete=0";

		list = this.getHibernateTemplate().find(hql);
		return list;
	}

	@Override
	public Long countResourceByProductId(Long proId) {
	
		String hql = "select count(r) from Resources r where  r.proId=" + proId
				+ " and r.isdelete=0";

		List list = this.getHibernateTemplate().find(hql);
		return new Long(list.get(0).toString());
	}

}
